package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import sun.swing.table.DefaultTableCellHeaderRenderer;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Enumeration;
import java.util.Vector;

public class ShowReadrInfo extends JInternalFrame {
    JTextField searchText = new JTextField(20);
    JButton searchBut = new JButton("搜索");
    JTable ReaderTable;
    JTable BorrowTable;
    JScrollPane tableScrollPane;
    String searchCondition1;
    DefaultTableModel tableModel;
    DefaultTableModel tableModel2;

    public ShowReadrInfo() {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        getContentPane().setLayout(new GridLayout(2,1));  //设置布局
        setBounds(10, 10, 1550, 600);
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setTitle("浏览图书");

        // 建立表格
        Vector header1, columns1;
        header1 = Dao.ReaderHead(false);
        columns1 = Dao.ReaderColumns();
        tableModel = new DefaultTableModel(columns1, header1);
        //绘制表格
        ReaderTable = new JTable(tableModel);
        ReaderTable.setAutoCreateRowSorter(true);//设置点击表头可排序
        tableScrollPane = new JScrollPane(ReaderTable);
        ReaderTable.setRowHeight(25);//设置行高
        ReaderTable.setFont(new Font("宋体", Font.PLAIN, 20));//行字体大小
        ReaderTable.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 20));//表头字体大小
        //FitTableColumns(ReaderTable);// 表格自适应列宽
        DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();//表格内容剧中
        thr.setHorizontalAlignment(JLabel.CENTER);
        ReaderTable.getTableHeader().setDefaultRenderer(thr);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);
        ReaderTable.setDefaultRenderer(Object.class, tcr);
        ReaderTable.addMouseListener(new tableAction());
        //把表格滚动面板加入主面板
        getContentPane().add(tableScrollPane);

        //添加ISBN和书名搜索栏面板


        header1 = Dao.borrowHead(false);
        columns1 = Dao.borrowColumns("");
        tableModel2 = new DefaultTableModel(columns1, header1);
        BorrowTable = new JTable(tableModel2);
        BorrowTable.setAutoCreateRowSorter(true);
        JScrollPane borrowpanel = new JScrollPane(BorrowTable);
        BorrowTable.setRowHeight(25);
        BorrowTable.setFont(new Font("宋体", Font.PLAIN, 20));//行字体大小
        BorrowTable.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 20));
        //FitTableColumns(BorrowTable);
        BorrowTable.getTableHeader().setDefaultRenderer(thr);
        BorrowTable.setDefaultRenderer(Object.class, tcr);
        getContentPane().add(borrowpanel);
        //初始化下拉列表框模型类


    }

    private class searchbooksBut implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            Vector header1, columns1;
            header1 = Dao.borrowHead(false);
            columns1 = Dao.borrowColumns(searchText.getText());
            tableModel.setDataVector(columns1, header1);
            //FitTableColumns(BorrowTable);// 表格自适应列宽
            DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();//表格内容剧中
            thr.setHorizontalAlignment(JLabel.CENTER);
            BorrowTable.getTableHeader().setDefaultRenderer(thr);
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
            tcr.setHorizontalAlignment(JLabel.CENTER);
            BorrowTable.setDefaultRenderer(Object.class, tcr);
        }
    }

    // 实现对表格进行自适应
    public void FitTableColumns(JTable myTable) {
        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) myTable.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(myTable, column.getIdentifier()
                            , false, false, -1, col).getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,
                        myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width);
        }
    }

    private class tableAction implements MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {
            int rows = ReaderTable.getSelectedRow();
            int cols = ReaderTable.getSelectedColumn();
            if (cols == 0) {
                searchText.setText((String) ReaderTable.getValueAt(rows, cols));
                Vector header1, columns1;
                header1 = Dao.borrowHead(false);
                columns1 = Dao.borrowColumns(searchText.getText());
                tableModel2.setDataVector(columns1, header1);
                //FitTableColumns(BorrowTable);// 表格自适应列宽
                DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();//表格内容剧中
                thr.setHorizontalAlignment(JLabel.CENTER);
                BorrowTable.getTableHeader().setDefaultRenderer(thr);
                DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
                tcr.setHorizontalAlignment(JLabel.CENTER);
                BorrowTable.setDefaultRenderer(Object.class, tcr);
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }
}
